<?php
/**
 * Copyright (c) 2006-2008, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @link         http://feather-cm.googlecode.com/
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

abstract class Avatar
{
	public    $root          = WWW_ROOT;
	protected $basePath      = 'files/avatars/';
	protected $width         = 50;
	protected $height        = 50;
	protected $crop          = true;
	protected $defaultAvatar = '_default-avatar.png';
	protected $inputName     = 'avatar';
	
	protected function __construct() {}
	
	function exists($name, $width=null, $height=null)
	{
		return (file_exists($this->path($name, 'jpg', $width, $height))
			or  file_exists($this->path($name, 'png', $width, $height))
			or  file_exists($this->path($name, 'gif', $width, $height)));
	}
	
	function url($name, $width=null, $height=null)
	{
		if ($width and $height and !$this->exists($name, $width, $height) and $this->exists($name))
		{
			# creates a resampled avatar
			Load::libs('resampler');
			$Resampler = new Resampler($this->path($name));
			$Resampler->create($this->path($name, 'jpg', $width, $height), $width, $height, $this->crop);
		}
		return '/'.$this->_path($name, null, $width, $height);
	}
	
	function path($name, $ext=null, $width=null, $height=null)
	{
		return $this->root.$this->_path($name, $ext, $width, $height);
	}
	
	function save($name)
	{
		Load::libs('image_upload');
		$upload = new ImageUpload(UPLOAD_NOT_ANIMATED);
		$ret    = $upload->exists($this->inputName);
		
		if ($ret === UPLOAD_ERR_OK)
		{
			# removes (any) previous avatar
			$this->delete($name);
			
			# saves new avatar
			return $upload->save($this->inputName, $this->root.$this->basePath.strtolower($name), $this->width, $this->height, $this->crop);
		}
		throw new ImageUploadException($ret);
	}
	
	function delete($name)
	{
		if (empty($name))
			return;
		
		# removes the avatar (one max)
		$files = glob($this->root.$this->basePath."$name.{jpg,gif,png}", GLOB_BRACE | GLOB_NOSORT);
		foreach($files as $f)
		{
			unlink ($f);
		}
		
		# removes any resampled avatar
		$files = glob($this->root.$this->basePath."*/$name.{jpg,gif,png}", GLOB_BRACE | GLOB_NOSORT);
		foreach($files as $f)
		{
			unlink ($f);
		}
	}
	
	function _path($name, $ext=null, $width=null, $height=null)
	{
		$name     = strtolower($name);
		$basePath = ($width and $height) ? $this->basePath."{$width}x{$height}".DS : $this->basePath;
		
#		var_dump("_path: $name, $ext, $width, $height; $basePath");
		
		if (is_null($ext))
		{
			$fname = getImage($this->root.$basePath.$name);
			if (!empty($fname))
				return $basePath.basename($fname);
			return $basePath.$this->defaultAvatar;
		}
		return $basePath."$name.$ext";
	}
}

class MemberAvatar extends Avatar
{
	protected $basePath  = 'files/avatars/members/';
	protected $inputName = 'avatar';
	private static $instance;
	
	function instance()
	{
		if (!isset(self::$instance))
			self::$instance = new self;
		return self::$instance;
	}
}
?>